/*******************************************************************************
 FileName:      streamBuff.h
 Author:        urey
 Version :      1.0
 Date:          2015年8月4日
 Description:
 History:
 <author>		<time>			<version >      <desc>
 urey        2015年8月4日          1.0          create
 *******************************************************************************/
#ifndef STREAMBUFF_H_
#define STREAMBUFF_H_

#include "ureydev_LinkedList/linkedList.h"
#include "apr_Lib/include/apr_general.h"
#include "apr_Lib/include/apr_pools.h"
#include <assert.h>

/* #define STREAMBUFF_BASE         STREAMBUFF_BASE */
#define STREAMBUFF_SUCCESS          0
#define STREAMBUFF_BASE             (int)0x85200000
#define STREAMBUFF_NULL_ERROR       (STREAMBUFF_BASE + 0x01)    /* 空指针错误，传入的参数存在空指针 */
#define STREAMBUFF_MEM_ERROR        (STREAMBUFF_BASE + 0x02)    /* 内存分配错误 */
#define STREAMBUFF_NULLNODE_ERROR   (STREAMBUFF_BASE + 0x03)    /* 空节点错误，从链表中读取到空节点，可能需要重建缓存来解决错误 */
#define STREAMBUFF_EXPEMPTY_ERROR   (STREAMBUFF_BASE + 0x04)    /* 异常空链表错误，当链表不当为空时为空，可能需要重建缓存来解决错误 */
#define STREAMBUFF_TAG               "STREAMBUFF_TAG"

typedef int                     Int32;
typedef unsigned int            Uint32;
typedef unsigned short          Uint16;
typedef S_LinkedList            S_StreamList;
typedef S_LinkedListNode        S_StreamListNode;
typedef struct S_StreamNode_    S_StreamNode;
typedef apr_pool_t              S_StmMemPool;

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif /* max */
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif /* min */

struct S_StreamNode_        /* 流存储节点，存储需要发送的数据流 */
{
    S_StreamListNode StreamLinkNode;
    Int32 startPos;
    Int32 endPos;
};

typedef struct S_StreamBuff_
{
    Uint32  totStoreSize;
    S_StreamList waitStreamList;
    S_StreamList freeStreamList;
    S_StmMemPool* pStreamPool;
    Uint16  buffSize;
}S_StreamBuff;


#define streamListCreate(pStreamList) linkedListCreate(pStreamList)

static inline void streamBuffCreate(S_StreamBuff** ppStreamBuff, Uint16 buffSize, S_StmMemPool* ParentPool)
{
    apr_initialize();
    S_StmMemPool* pStreamPool = NULL;
    apr_pool_create(&pStreamPool, ParentPool);
    S_StreamBuff* pStreamBuff = NULL;
    pStreamBuff = apr_palloc(pStreamPool, sizeof(S_StreamBuff));
    pStreamBuff->totStoreSize = 0;
    pStreamBuff->buffSize = buffSize;
    pStreamBuff->pStreamPool = pStreamPool;
    streamListCreate(&pStreamBuff->waitStreamList);
    streamListCreate(&pStreamBuff->freeStreamList);
    *ppStreamBuff = pStreamBuff;
    return;
}

static inline void streamBuffDelete(S_StreamBuff* pStreamBuff)
{
    S_StmMemPool* pStreamPool = pStreamBuff->pStreamPool;
    memset(pStreamBuff, 0, sizeof(S_StreamBuff));
    apr_pool_destroy(pStreamPool);
    return;
}

Int32 addStreamDataCore(S_StreamBuff* pStreamBuff, char* pData, Int32 DataLen);
Int32 readStreamDataCore(S_StreamBuff* pStreamBuff, Int32 offset, char* pData, Int32 readDataLen);
Int32 deleteStreamDataCore(S_StreamBuff* pStreamBuff, Int32 DataLen);


#endif /* STREAMBUFF_H_ */
